﻿
function setSelectList(selectId)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.multiple="multiple";
}
function setSelectSelected(selectId,value,isChange)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	for(var i=0;i<select.options.length;i++)
	{
		if(value == select.options[i].value)
		{
			select.selectedIndex=i;
			if(isChange==true){select.onchange();}
			return;
		}
	}
}
function setSelectSelectedIndex(selectId,index,isChange)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.selectedIndex=index;
	if(isChange==true){select.onchange();}
}
function setSelectOptions(selectId,options)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options.length=0;
	for(var i=0;i<options.length;i++)
	{
		var optionData=options[i];
		select.options.add(new Option(optionData.text,optionData.value)); 
	}
}
function setSelectOptionsWithJson(selectId,options)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options.length=0;
	for(var item in options)
	{
		select.options.add(new Option(options[item],item)); 
	}
}
function setSelectOptionsWithString(selectId,strs,sep1,sep2)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	var options=select.options;
	if((undefined == sep1)||("" == sep1)){sep1=";";}
	if((undefined == sep2)||("" == sep2)){sep2=",";}
	var arr=strs.split(sep1);
	for(var i=0;i<arr.length;i++)
	{
		var op=arr[i];
		if((undefined != op)&&(op.Trim().length>2))
		{
			var text=op.split(sep2)[1];
			var value=op.split(sep2)[0];
			if((undefined != value)&&(value.Trim().length>0)&&(undefined != text)&&(text.Trim().length>0))
			{
				addSelectOption(select,value,text);
			}
		}
	}
	return strs;
}
function addSelectOption(selectId,value,text)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	for(var i=0;i<select.options.length;i++)
	{
		if(value == select.options[i].value)
		{
			return;
		}
	}
	select.options.add(new Option(text,value)); 
}
function removeSelectOptions(selectId)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options.length=0;
}
function removeSelectOption(selectId,value)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	for(var i=0;i<select.options.length;i++)
	{
		if(value == select.options[i].value)
		{
			select.options.remove(i);
			return;
		}
	}
}
function removeSelectOptionIndex(selectId,index)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options.remove(indx);
}
function removeSelectSelected(selectId)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options.remove(select.selectedIndex);
}
function getSelectSelectedOption(selectId)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	return select.options[select.selectedIndex];
}
function getSelectOptions(selectId)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	return select.options;
}
function getSelectIndexWithValue(selectId,value)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	for(var i=0;i<select.options.length;i++)
	{
		if(value == select.options[i].value)
		{
			return i;
		}
	}
	return -1;
}
function getSelectOptionsString(selectId,sep1,sep2)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	var options=select.options;
	var strs="";
	if((undefined == sep1)||("" == sep1)){sep1=",";}
	if((undefined == sep2)||("" == sep2)){sep2=";";}
	for(var i=0;i<options.length;i++)
	{
		var op=options[i];
		strs=strs+op.value+sep1+op.text+sep2;
	}
	return strs;
}
function eidtSelectSelectedOption(selectId,value,text)
{
	var select=selectId;
	if(typeof(selectId)=="string"){select=document.getElementById(selectId)};
	select.options[select.selectedIndex] = new Option(text,value); 
}
/**
var json={'sid':'id1','did':'id2','items':[
	{'value':'v1','text':'xx1','options':[
							{'value':'bv1','text':'b值1'},
							{'value':'bv12','text':'b值12'}]},
	{'value':'v2','text':'xx2','options':[
							{'value':'bv2','text':'b值1222'},
							{'value':'bv211','text':'b值12225555'},
							{'value':'bv211111','text':'b值122555555552'},
							{'value':'bv22','text':'b值1222222'}]},
	{'value':'v3','text':'xx3','options':[
							{'value':'bv1','text':'b值1333'},
							{'value':'bv1','text':'b值13333333333333333333'}]}
	
	]}
new SelectRelate(json);

**/
function SelectRelate(jsonObject)
{
	var sSelect=document.getElementById(jsonObject.sid);
	removeSelectOptions(jsonObject.sid);
	var items=jsonObject.items;
	for(var i=0;i<items.length;i++)
	{
		if(undefined != items[i].value )
		{
			addSelectOption(sSelect,items[i].value,items[i].text);
		}
	}
	sSelect.onchange=function()
	{
		for(var i=0;i<items.length;i++)
		{
			if(sSelect.value == items[i].value)
			{
				setSelectOptions(jsonObject.did,items[i].options);
				break;
			}
		}
	}
	setSelectOptions(jsonObject.did,items[0].options);
}
/**
var jsonObject={'sid':'ss1','options':[
	{'value':'v1','fields':[{'id':'t1','value':'111111'},{'id':'t2','value':'111111111111111111111'}]},
	{'value':'v2','fields':[{'id':'t1','value':'222222'},{'id':'t2','value':'2222222222222222222'}]},
	{'value':'v3','fields':[{'id':'t1','value':'33'},{'id':'t2','value':'333333333333333333333333333333333333333333'}]}
]};
new  SelectRelateField(jsonObject);
*/
function SelectRelateField(jsonObject)
{
	var sSelect=document.getElementById(jsonObject.sid);
	sSelect.onchange=function()
	{
		var ops=jsonObject.options;
		for(var i=0;i<ops.length;i++)
		{
			if(sSelect.value == ops[i].value)
			{
				var fields=ops[i].fields;
				for(var j=0;j<fields.length;j++)
				{
					if(document.getElementById(fields[j].id)){document.getElementById(fields[j].id).value=fields[j].value;}
				}
				break;
			}
		}
	}
}

